package cn.juque.systemservice.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.thread.BaseOperatorThreadLocal;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.systemservice.api.dto.role.*;
import cn.juque.systemservice.api.provider.IRoleInfoProvider;
import cn.juque.systemservice.domain.RoleFuncRelationDO;
import cn.juque.systemservice.entity.RoleInfo;
import cn.juque.systemservice.service.IRoleInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>角色信息</li>
 * </ul>
 * @date 2022-11-03 23:57:34
 **/
@Slf4j
@Service("roleInfoProvider")
@HandlerAppException
public class RoleInfoProviderImpl implements IRoleInfoProvider {

    @Resource
    private BaseOperatorThreadLocal baseOperatorThreadLocal;

    @Resource
    private IRoleInfoService roleInfoService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDTO 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<RoleInfoListDTO>> list(RoleInfoSearchDTO searchDTO) {
        this.validatorUtil.checkNotNull(searchDTO);
        PageInfo pageInfo = searchDTO.getPageInfo();
        RoleInfo roleInfo = new RoleInfo();
        BeanUtil.copyProperties(searchDTO, roleInfo);
        List<RoleInfo> infoList;
        if (null == pageInfo) {
            infoList = this.roleInfoService.listInfoWithoutPage(roleInfo);
        } else {
            infoList = this.roleInfoService.listInfoWithPage(pageInfo, roleInfo);
        }
        List<RoleInfoListDTO> infoDtoList = BeanUtil.copyToList(infoList, RoleInfoListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, infoDtoList, pageInfo);
    }

    /**
     * 查询功能列表
     *
     * @param searchDTO 入参
     * @return list
     */
    @Override
    public BaseResponseDTO<List<RoleFuncListDTO>> listFunc(FuncIdSearchDTO searchDTO) {
        List<RoleFuncRelationDO> list = this.roleInfoService.listFuncByRoleId(searchDTO.getRoleId());
        List<RoleFuncListDTO> dtoList = BeanUtil.copyToList(list, RoleFuncListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, dtoList);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return RoleInfoDetailDTO
     */
    @Override
    public BaseResponseDTO<RoleInfoDetailDTO> detail(String id) {
        RoleInfo roleInfo = this.roleInfoService.detail(id);
        RoleInfoDetailDTO detailDTO = new RoleInfoDetailDTO();
        BeanUtil.copyProperties(roleInfo, detailDTO);
        return new BaseResponseDTO<>(MessageEnum.OK, detailDTO);
    }

    /**
     * 新增
     *
     * @param addDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> add(RoleInfoAddDTO addDTO) {
        this.validatorUtil.fastValidate(addDTO);
        this.baseOperatorThreadLocal.set(addDTO.getBaseOperator());
        RoleInfo roleInfo = new RoleInfo();
        BeanUtil.copyProperties(addDTO, roleInfo);
        this.roleInfoService.saveInfo(roleInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 添加功能
     *
     * @param addDTO dto
     * @return boolean
     */
    @Override
    public BaseResponseDTO<Boolean> addFunc(RoleRelFuncAddDTO addDTO) {
        this.roleInfoService.saveFuncRelation(addDTO.getRoleId(), addDTO.getFuncIdList());
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 更新
     *
     * @param updateDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> update(RoleInfoUpdateDTO updateDTO) {
        this.validatorUtil.fastValidate(updateDTO);
        this.baseOperatorThreadLocal.set(updateDTO.getBaseOperator());
        RoleInfo roleInfo = new RoleInfo();
        BeanUtil.copyProperties(updateDTO, roleInfo);
        this.roleInfoService.updateInfo(roleInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 删除
     *
     * @param req 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> delete(RoleInfoDeleteDTO req) {
        List<String> infoIdList = req.getIdList();
        this.roleInfoService.deleteInfo(infoIdList);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }
}
